﻿using System.Collections.ObjectModel;
using System.Threading;
using System.Windows;
using MDCAdminTool;
using Microsoft.ApplicationServer.Caching.AdminApi;
using MDCAdminTool.Commands;
using System.Windows.Input;
using System.Reflection;
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Diagnostics;
using System.Management.Automation.Runspaces;
using System.Management.Automation.Remoting;

namespace IntegrationTests
{
    public class BaseTest
    {
        public static String[] VMs = new String[] { "VAT-IT-C1N1", "VAT-IT-C1N2" };
        public static String[] ClusterConfigVMs = new String[] { "VAT-IT-Configs" };
        public static String[] AllVMs = VMs.Concat(ClusterConfigVMs).ToArray();

        public static Dictionary<String, int>
            HostDescriptors = new Dictionary<string, int>() 
            {
                { "VAT-IT-C1N1", 22233 },
                { "VAT-IT-C1N2", 22233 }
            };


        public static void pingPS(String hostName) {
            Debug.WriteLine("Ping vm: " + hostName);
            Uri serverUri = new Uri("http://" + hostName + ":5985/wsman");
            using (Runspace rs = RunspaceFactory.CreateRunspace(new WSManConnectionInfo(serverUri)))
            {
                rs.Open();
            }
        }

        public static void AwaitVMs()
        {
            while (true)
            {
                try
                {
                    foreach (String vm in VMs)
                    {
                        pingPS(vm);
                    }
                    return;
                }
                catch (PSRemotingTransportException)
                {
                    Thread.Sleep(2000);
                }
            }
        }

        public static void RevertVMs()
        {
            foreach (String vm in AllVMs)
            {
                //VMHelper.Revert(vm);
            }
        }

        //public static void CleanXMLCluster()
        //{
        //    Process process = new Process();
        //    process.StartInfo.FileName = "robocopy";
        //    process.StartInfo.CreateNoWindow = true;
        //    process.StartInfo.Arguments = "\"\\\\VAT-CONFIGS\\ConfigTest - Copy\" \\\\VAT-CONFIGS\\ConfigTest";
        //    process.StartInfo.UseShellExecute = false;
        //    process.StartInfo.RedirectStandardOutput = true;
        //    process.StartInfo.RedirectStandardError = true;
        //    process.Start();
        //    process.WaitForExit();
        //    if (process.ExitCode != 0) throw new Exception("Script execution failed: " +
        //         "stderr: " + process.StandardError.ReadToEnd() +
        //         "stdout: " + process.StandardOutput.ReadToEnd());

        //}

        private static Collection<CacheCluster> _CacheClusters;

        public BaseTest()
        {
        }

        public static Collection<CacheCluster> Clusters
        {
            get
            {
                AppInitialize();
                if (_CacheClusters != null) return _CacheClusters;
                Clusters clusters = new Clusters();
                clusters.Logger = new Logger();
                clusters.LoadFromConfiguration();
                App.Clusters = clusters;
                _CacheClusters = clusters;
                return _CacheClusters;;
            }
            set
            {
                _CacheClusters = value;
                
            }
        }



        public static CacheCluster Cluster
        {
            get
            {
                return Clusters[0];
            }
        }

        public static Collection<CacheHost> Hosts(CacheCluster cacheCluster = null)
        {
                CacheCluster cluster = Clusters.Single<CacheCluster>();
                cluster.LoadCacheHosts();
                return cluster.Hosts;
        }



        private static void ReInitialize()
        {
            //AppDomain.CreateDomain(
            //if (Application.Current != null)
              //  Application.Current.Shutdown();
            //AppInitialize();
        }

        private static void AppInitialize()
        {
            //AppDomain.CreateDomain("appdomain");
            //AppDomain.

            App application;
            if (Application.Current == null)
            {
                application = new App();
                application.InitializeComponent();
                new MainWindow();
            }
            //else application = (App)Application.Current;
        }

        //protected void WaitForStatus(CacheCluster cacheCluster, ServiceStatus status)
        //{
        //    while (true)
        //    {
        //        if (cacheCluster.Status.Equals(status)) return;
        //        Thread.Sleep(1000);
        //    }
        //}

        //protected void WaitForStatus(CacheHost cacheHost, ServiceStatus status)
        //{
        //    while (true)
        //    {
        //        if (cacheHost.Status.Equals(status)) return;
        //        Thread.Sleep(1000);
        //    }
        //}

        public static ExecutedRoutedEventArgs makeExecutedRoutedEventArgs(AsyncCommandBindingBase_Accessor target, ICacheCommand service)
        {
            ICommand ribbonCommand = ((System.Windows.Input.CommandBinding)(((Microsoft.VisualStudio.TestTools.UnitTesting.BaseShadow)(target)).Target)).Command;
            ConstructorInfo ci = typeof(ExecutedRoutedEventArgs).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { typeof(ICommand), typeof(object) }, null);
            ExecutedRoutedEventArgs e = (ExecutedRoutedEventArgs)ci.Invoke(new object[] { ribbonCommand, service });
            return e;
        }

        public static CanExecuteRoutedEventArgs makeCanExecuteRouteEventArgs(AsyncCommandBindingBase_Accessor target, ICacheCommand service)
        {
            ICommand ribbonCommand = ((System.Windows.Input.CommandBinding)(((Microsoft.VisualStudio.TestTools.UnitTesting.BaseShadow)(target)).Target)).Command;
            ConstructorInfo ci = typeof(CanExecuteRoutedEventArgs).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { typeof(ICommand), typeof(object) }, null);
            CanExecuteRoutedEventArgs e = (CanExecuteRoutedEventArgs)ci.Invoke(new object[] { ribbonCommand, service });
            return e;
        }
        
    }
}
